package juc.flowcontrol.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author fangbin
 * @desc
 * @date 2020/6/26 17:31
 */
public class ConditionDemo {

    private ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void method1() throws InterruptedException {
        lock.lock();
        try{
            System.out.println("条件满足开始await");
            condition.await();
            System.out.println("条件满足开始接着执行");
        }finally {
            lock.unlock();
        }
    }

    public void method2(){
        lock.lock();
        try{
            System.out.println("开始唤醒其他线程");
            condition.signal();
        }finally {
            lock.unlock();
        }

    }

    public static void main(String[] args) throws InterruptedException {
        ConditionDemo conditionDemo = new ConditionDemo();

        new Thread(new Runnable() {
            @Override
            public void run() {
                conditionDemo.method2();
            }
        }).start();
        conditionDemo.method1();

    }

}
